package com.hbnu.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import javax.xml.crypto.Data;
import java.beans.PropertyVetoException;

@Configuration //配置类
@ComponentScan("com.hbnu") //spring扫描指定位置的所有类和接口,如果有指定注解就创建对象
@EnableAspectJAutoProxy //自动开启AOP代理
public class ApplicationConfig {
    @Bean
    public DataSource dataSource() throws PropertyVetoException{
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/class2110?serverTimezone=GMT&useSSL=false");
        dataSource.setUser("root");
        dataSource.setPassword("123456");

        return dataSource;
    }

    @Bean
    //将数据源对象注入到JDBCTemplate对象中
    public JdbcTemplate jdbcTemplate() throws PropertyVetoException {
        return new JdbcTemplate(dataSource());
    }

    @Bean
    //加事务管理器，还得通过注解开启事务管理
    public PlatformTransactionManager platformTransactionManager() throws PropertyVetoException {
        return new DataSourceTransactionManager(dataSource());
    }
}
